package cn.huhst.cas.weisiqian.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import cn.huhst.cas.weisiqian.pojo.Comment;
import cn.huhst.cas.weisiqian.service.CommentService;
import cn.huhst.cas.weisiqian.util.JSON;

@WebServlet("/Like.do")
public class Like extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(Like.class);
	private CommentService commentservice = null;

	public Like() {
		super();
		commentservice = new CommentService();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String commentid = request.getParameter("commentid");
		JSON json = new JSON();

		if (commentid != null) {
			json = Comment.checkCommentid(commentid, json);
			if (json.isEmpty()) {
				boolean result = commentservice.Like(commentid);
				if (result) {
					json.putOKMSG(commentid + "点赞成功");
					logger.info(commentid + "点赞成功");
				} else {
					json.putErrorMSG("点赞失败");
					logger.error(commentid + "点赞失败");
				}
			} else {
				json.putErrorMSG("评论ID不合法");
				logger.error("json:" + json);
			}
		} else {
			json.putErrorMSG("评论ID为null");
			logger.error("评论ID为null，点赞失败 ");
		}

		response.setCharacterEncoding("UT-8");
		PrintWriter out = response.getWriter();
		out.print(json);
	}

}
